package com.travel.WeChatPay;

import com.travel.config.PayInfoConfig;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.Signature;

/**
 *
 * @author Rick chou
 * @date 2024/7/16 16:34
 * WXPayUtil
 *
 */
@Component
public class WXPayUtil {
    private static PayInfoConfig payInfoConfig;  // 仍然是静态的，但不使用 @Resource 直接注入

    @Autowired
    public WXPayUtil(PayInfoConfig payInfoConfig) {
        WXPayUtil.payInfoConfig = payInfoConfig; // **手动赋值给静态变量**
    }




    public static String getSign(String signature,String privateKey) throws Exception {
        //replace 根据实际情况，不一定都需要
        try {
            PrivateKey Key = payInfoConfig.getPrivateKey(privateKey);
            Signature sign = Signature.getInstance("SHA256withRSA");
            sign.initSign(Key);
            sign.update(signature.getBytes(StandardCharsets.UTF_8));
            return Base64.encodeBase64String(sign.sign());
        }catch (Exception e){
            throw new Exception();
        }
    }
}
